<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/7 0007
 * Time: 上午 9:38
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class UserManage extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->helper('url');
    }

    /**
     * 后台会员管理
     *
     * */
    public function index($offset='')
    {
        //调用user_model类
        $this->load->model('User_model');
        //每页条数
        $page_size = 3;
        //echo $this->User_model->countRows();die;
        //配置分页信息
        $this->load->library('pagination');
        $config['base_url'] = site_url('admin/userManage/index');
        $config['total_rows'] = $this->User_model->countRows();
        $config['per_page'] = $page_size;
        //$config['uri_segment'] = 4;
        #自定义分页链接
        $config['first_link'] = '首页';
        $config['last_link'] = '尾页';
        $config['prev_link'] = '上一页';
        $config['next_link'] = '下一页';
        //初始化分页类
        $this->pagination->initialize($config);

        //生成分页信息
        $data['link'] = $this->pagination->create_links();

        $limit = $config['per_page'];
        //使用User_model类的方法,查询数据
        $data['user_data'] = $this->User_model->checkAll($limit,$offset);
        //渲染
        $this->load->view('admin/user/userIndex',$data);
    }

    /**
     *用户添加
     * */
    public function addUser()
    {
        $this->load->model('Area_model');
        //获取省
        $province = $this->Area_model->getProvince($parentid=0);
        //ajax传值获取城市,县区
        if ($_GET && count($_GET)>0) {
            $type = $_GET['type'];
            switch ($type) {
                case 'city' :
                    $id = $_GET['id'];
                    $city = $this->Area_model->getProvince($parentid = $id);
                    //获取组装数据
                    $city_data = $this->Area_model->checkData($city);
                    $ret = ['status' => 'success', 'message' => $city_data];
                    echo json_encode($ret);
                    die;
                    break;

                case 'country':
                    $id = $_GET['id'];
                    $country = $this->Area_model->getProvince($parentid = $id);
                    //获取组装数据
                    $country_data = $this->Area_model->checkData($country);
                    //print_r($country_data);die;
                    $ret = ['status' => 'success', 'message' => $country_data];
                    echo json_encode($ret);
                    die;
                    break;


            }

        }
        //echo $pro_data;die;
        //添加新的用户数据
        if($_POST && count($_POST)>0)
        {
            //print_r($_POST);die;
            $this->load->model('User_model');
            $res = $this->User_model->addUser($_POST);
            if($res)
            {
                redirect('admin/userManage/index');
            }
            else
            {
                redirect('admin/userManage/addUser');
            }
            //print_r($res);die;
        }
        $this->load->view('admin/user/addUser', ['pro_data' => $province]);
    }
    /**
     * 图片上传
     * */
    public function imgUpload()
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '10000';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        $this->load->library('upload', $config);
        if($this->upload->do_upload('file'))
        {
            $res = $this->upload->data();
            //ajax返回信息
            $path = base_url().'uploads/'.$res['file_name'];
            $ret = ['status'=>'success','message'=>$path];
        }
        else
        {
            //echo $this->upload->display_errors();
            $error = $this->upload->display_errors();
            $ret = ['status'=>'false','message'=>$error];
        }
        //die;
        echo json_encode($ret);
        die;
    }

    /**
     * 用户操作
     * */
    public function operate()
    {
        $this->load->model('User_model');
        $type = $_POST['type'];
        switch($type)
        {
            case 'del' :
                $id = $_POST['id'];
                $res = $this->User_model->delUser($id);
                echo $this->User_model->message($res);
                die;
            break;
        }
    }





}



